/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 jfinal app. jfapp Group.
 */

package app.models.activity;

import com.github.sog.annotation.TableBind;
import com.github.sog.config.StringPool;
import com.github.sog.controller.datatables.core.ColumnDef;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.google.common.base.Strings;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * <p>
 * The table ol_brand_activity mapping model.
 * 品牌活动
 * </p>
 */
@TableBind(tableName = "ol_brand_activity")
public class BrandActivity extends Model<BrandActivity> {

    private static final long serialVersionUID = 1L;
    /**
     * The public dao.
     */
    public static final BrandActivity dao = new BrandActivity();


    public List<BrandActivity> list() {
        return find(SqlKit.sql("brandactivity.list"));
    }


    public Page<BrandActivity> datatable(DatatablesCriterias criterias, String name, Integer para_brand) {
        StringBuilder sb = new StringBuilder(" WHERE 1=1 ");
        if(!Strings.isNullOrEmpty(name)){
            sb.append(" AND ol_brand_activity.name like '%").append(name).append("%'");
        }
        if(para_brand>0){
            sb.append("AND ol_brand_activity.id IN (SELECT activity FROM ol_brandrf_activity WHERE brand =").append(para_brand).append(")");
        }

        int pageSize = criterias.getDisplaySize();
        int start = criterias.getDisplayStart() / pageSize + 1;
        final List<ColumnDef> sortingColumnDefs = criterias.getSortingColumnDefs();

        String where = " FROM ol_brand_activity LEFT JOIN ol_brandactivity_category ON ol_brand_activity.category = ol_brandactivity_category.id";
        where += sb.toString();
        System.out.println("----------------------------------" + where);
        if (sortingColumnDefs != null && !sortingColumnDefs.isEmpty()) {
            StringBuilder orderBy = new StringBuilder();
            for (ColumnDef sortingColumnDef : sortingColumnDefs)
                if (sortingColumnDef.isSortable()) {
                    orderBy.append(sortingColumnDef.getName()).append(StringPool.SPACE).append(sortingColumnDef.getSortDirection().toString());
                }
            final String byColumns = orderBy.toString();
            if (!Strings.isNullOrEmpty(byColumns)) {
                where += " ORDER BY " + byColumns;
            }
        }
        if (!where.contains("ORDER")) {
            where += " ORDER BY id DESC";
        }

        return paginate(start, pageSize, SqlKit.sql("brandactivity.columns"), where);
    }

    /**
     * 批量停用
     */
    public boolean batch_stop(List<String> ids) {
        Object[][] objects = new Object[ids.size()][];
        Object[] obj;
        for (int i = 0; i < ids.size(); i++) {
            obj = new Object[1];
            obj[0] = ids.get(i);
            objects[i] = obj;
        }
        try {
            Db.batch(SqlKit.sql("brandactivity.stop"), objects, 50);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public List<BrandActivity> findByBrandId(long brandid) {
        return find(SqlKit.sql("brandactivity.findByBrandId"), brandid);
    }
}